<template>
  <div class="BOMCengJiCY">
    <basic-container>
      <avue-crud
        ref="crud"
        :option="option"
        :table-loading="listLoading"
        :data="list"
        :page.sync="page"
        @search-change="handleFilter"
        @selection-change="selectionChange"
        @refresh-change="refreshChange"
        @on-load="getList">
        <template slot="searchMenu" slot-scope="{row,size}">
          <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm)}" icon="el-icon-delete">清空</el-button>
        </template>
        <template slot="search" slot-scope="{row,size}" >
          <el-form-item label="充值差异金额" labelWidth="150px" style="width:450px">
            <el-input-number v-model="searchForm.diffInvestStartAmount" :min="0" :max="99999998" label="差异金额范围" clearable></el-input-number>~
            <el-input-number v-model="searchForm.diffInvestEndAmount" :min="1" :max="99999999" label="差异金额范围" clearable></el-input-number>
          </el-form-item>
          <el-form-item label="售票差异金额" labelWidth="150px" style="width:450px">
            <el-input-number v-model="searchForm.diffSelltkStartAmount" :min="0" :max="99999998" label="差异金额范围" clearable></el-input-number>~
            <el-input-number v-model="searchForm.diffSelltkEndAmount" :min="1" :max="99999999" label="差异金额范围" clearable></el-input-number>
          </el-form-item>
          <el-form-item label="补票差异金额" labelWidth="150px" style="width:450px">
            <el-input-number v-model="searchForm.diffFaretkStartAmount" :min="0" :max="99999998" label="差异金额范围" clearable></el-input-number>~
            <el-input-number v-model="searchForm.diffFaretkEndAmount" :min="1" :max="99999999" label="差异金额范围" clearable></el-input-number>
          </el-form-item>
        </template>
        <template slot-scope="scope" slot="lineCodeSearch">
          <el-select v-model="searchForm.lineCode" placeholder="请选择" style="width: 150px" @change="changeLine" filterable>
            <el-option
              v-for="(item,index) in lineCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationCodeSearch">
          <el-select v-model="searchForm.stationCode" placeholder="请选择" style="width: 150px" @change="changeStation" filterable>
            <el-option
              v-for="(item,index) in stationCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="equipmentNumberSearch">
          <el-select v-model="searchForm.equipmentNumber" placeholder="请选择" style="width: 150px">
            <el-option
              v-for="(item,index) in equipmentNumberList"
              :key="index"
              :label="item.operationCode"
              :value="item.operationCode">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="conductorPersonCodeSearch">
          <el-select v-model="searchForm.conductorPersonCode" placeholder="请选择" style="width: 150px" filterable>
            <el-option
              v-for="(item,index) in userList"
              :key="index"
              :label="item.nikename"
              :value="item.userCode">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button type="text" size="small" v-if="scope.row.qfReplyStatus!=1" plain @click.stop="previewEditor(scope.row,1)">协查回馈</el-button>
          <el-button type="text" size="small" v-if="scope.row.qfReplyStatus==1" plain @click.stop="previewEditor(scope.row,2)">预览</el-button>
        </template>
        <template slot="menuRight">
          <el-button type="primary"  size="mini" @click="submit">提交</el-button>
<!--          <el-button type="primary"  size="mini" @click="batchFeedback">批量回馈差异原因</el-button>-->
          <!-- <Export style="display: inline-flex;padding: 0px;width: auto"></Export> -->
<!--          <el-button type="primary"  size="mini" @click="exportData">导出</el-button>-->
          <el-upload
            class="upload-inline"
            action=""
            :show-file-list="false"
            accept=".xls,.xlsx"
            :http-request="handleImport"
          >
            <el-button type="primary" size="mini">导入</el-button>
          </el-upload>
        </template>
      </avue-crud>
    </basic-container>
    <HuikuiInfo ref="HuikuiInfo" v-if="isShowDia"></HuikuiInfo>
    <HuikuiInfoBatchFeedback ref="HuikuiInfoBatchFeedback" v-if="isShowDiaBatchFeedback"></HuikuiInfoBatchFeedback>
  </div>

</template>

<script>
  import {getForm, postForm} from '@/api/gen/gen'
  import {mapGetters} from 'vuex';
  import HuikuiInfo from './HuikuiInfo';
  import HuikuiInfoBatchFeedback from './HuikuiInfoBatchFeedback';
  import {exportDataToExcel} from "../../../api/subwayRat/exportSelf";
  import {
    setSearchFiledsEmpty,
    getSwPmDeviceOperationCodeList,
    getStationByCode,
    getLineAndStationByCode,
    getSysUserToLS,
    getgetStationCodes, getSwPmTvmCodeList, getLineCodes
  } from "../../../api/subwayRat/commonCheck";
  import {tableOption,getSwAuConductorHierarchyAmountDifferenList,updateSwAuConductorHierarchyAmountDifferenToStatus,
    exportBOMJDorQF,importBOMJDorQF
  } from '../../../api/clarifyManagement/BOMCengJiCY';
  import Export from './Export';
  import { handleExcelResponse } from "@/util/util";
  export default {
    name: 'BOMCengJiCY',
    components:{
      HuikuiInfo,
      HuikuiInfoBatchFeedback,
      Export
    },
    data() {
      return {
        isShowDia:false,
        isShowDiaBatchFeedback:false,
        option: tableOption,
        listLoading: true,
        selectKeys:[], //存放对像
        selectKeyVals:[], //存放值
        ids : [],
        searchForm:{
          lineCode:'',
          stationCode:'',
          conductorPersonCode:'',
          equipmentNumber:'',
          qfReplyStatus:'0' //默认未回馈
        },
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条,
          isAsc: false// 是否倒序
        },
        list : [],
        userList:[],
        showType : 0,
        lineCodes : [],
        stationCodes : [],
        equipmentNumberList : [],
        batchFeedbackList : []
      }
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    watch: {
    },
    mounted() {
      getLineCodes().then(res=>{ //获取线路
        this.lineCodes=res;
      });
    },
    created() {
    },
    methods: {
      setSearchFiledsEmpty,
      refreshChange() {
        this.getList()
      },
      changeLine(v){ //改变线路时
        getgetStationCodes(v).then(res=>{
          this.stationCodes=res;
          this.searchForm.stationCode='';
          this.searchForm.conductorPersonCode = '';
          this.searchForm.equipmentNumber = '';
          this.equipmentNumberList = [];
          this.userList = [];
        })
      },
      async changeStation(v){ //改变车站
        let p = {
          line_code : this.searchForm.lineCode,
          station_code : v
        };
        await getSysUserToLS(p).then((response)=>{
          this.userList = response.data.data;
        });
        let res=[];
        await getSwPmDeviceOperationCodeList({lineCode:this.searchForm.lineCode,stationCode:v}).then(r=>{
          res=r.data.data;
        });
        this.searchForm.conductorPersonCode = '';
        this.searchForm.equipmentNumber = '';
        this.equipmentNumberList=res;
      },
      previewEditor(row,type){
        this.showType = type;
        this.isShowDia=true;
        this.$nextTick(()=>{
          this.$refs.HuikuiInfo.recallShow(row);
        })
      },
      async submit(){
        if(this.selectKeys.length<=0){
          this.$message.warning("请选择要回馈的数据！");
          return;
        }
        for(let i=0;i<this.selectKeys.length;i++){
          if(1 == this.selectKeys[i].qfReplyStatus){
            this.$message.warning("勾选数据中存在清分已回馈数据！");
            return
          }
        }
        let ids=[];
        for(let i=0;i<this.selectKeys.length;i++){
          ids.push(this.selectKeys[i].id);
        }
        let messageLSList = [];
        for(let i=0;i<this.selectKeys.length;i++){
          let data = await getLineAndStationByCode({"lineCode":this.selectKeys[i].lineCode, "code":this.selectKeys[i].stationCode});
          messageLSList.push({
            "content":"车站：" + data.data[0].name,
            "lineCode":this.selectKeys[i].lineCode,
            "stationCode":this.selectKeys[i].stationCode
          })
        }
        let p = {
          messageLSList:JSON.stringify(messageLSList),
          title:this.$store.getters.tag.label,
          msgType:"提交",
          type:"BOMCengJiCY_002",
          ids:ids,
          typeTwo:"2"
        };
        await updateSwAuConductorHierarchyAmountDifferenToStatus(p).then((response)=>{
        });
        this.$message.success("操作成功！");
        this.getList();
      },
      batchFeedback(){
        if(this.selectKeys.length<=0){
          this.$message.warning("请选择要回馈的数据！");
          return;
        }
        for(let i=0;i<this.selectKeys.length;i++){
          if(1 == this.selectKeys[i].qfReplyStatus){
            this.$message.warning("勾选数据中存在清分已回馈数据！");
            return
          }
        }
        this.batchFeedbackList = this.selectKeys;
        this.isShowDiaBatchFeedback = true;
      },
      selectionChange(list){
        this.selectKeys=list;
        this.selectKeyVals=[];
        for(let i=0;i<list.length;i++){
          this.selectKeyVals.push(list[i].id);
        }
      },
      // async exportData(){ // 导出
      //   this.searchForm.pageNo=1;
      //   this.searchForm.pageSize=1000000000;
      //   if(this.selectKeys.length>0){
      //     exportDataToExcel(this.selectKeys,this.option.column,'BOM层级差异');
      //   }else {
      //     let res=await getSwAuConductorHierarchyAmountDifferenList(this.searchForm);
      //     let data = res.data.data.list;
      //     exportDataToExcel(data,this.option.column,'BOM层级差异');
      //   }
      // },
      handleImport(item) {//导入
        let file = item.file;
        let param = new FormData();
        param.append("file", file);
        importBOMJDorQF(param).then(() => {
          this.$notify({
            title: "成功",
            message: "导入成功",
            type: "success",
            duration: 2000
          });
          this.getList();
        });
      },
      async exportData(){ //导出
        let loading = this.$loading({
          lock: true,
          text: "Loading",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)"
        })
        try {
          if(this.selectKeys.length>0){
            if(this.isqfReplyStatus()==false){
              this.$message.info("请选择未回馈的数据！");
              return;
            }
            const res = await exportBOMJDorQF({ids:this.selectKeyVals.join(",")});
            handleExcelResponse(res);
          }else{
            this.searchForm.assistState=2;
            this.searchForm.comparativeType = "2";
            this.searchForm.qfReplyStatus = "0";
            const res = await exportBOMJDorQF(this.searchForm);
            handleExcelResponse(res);
          }
        } finally {
          loading.close();
        }
      },

      isqfReplyStatus(){ // 是否回馈
        for(let i=0;i<this.selectKeys.length;i++){
          let row=this.selectKeys[i];
          if(row.qfReplyStatus==1){
            return false
          }
        }
        return true;
      },

      previewEditor(row,type){
        this.showType = type;
        this.isShowDia=true;
        this.$nextTick(()=>{
          this.$refs.HuikuiInfo.recallShow(row,this);
        })
      },
      getList() {
        this.listLoading = true;
        Object.assign(this.searchForm,this.page);
        this.searchForm.assistStates = "2,3";
        this.searchForm.comparativeType = "2";
        getSwAuConductorHierarchyAmountDifferenList(this.searchForm).then((response)=>{
          this.list = response.data.data.list;
          this.listLoading = false;
          this.page.total=response.data.data.pageInfo.total;
        });
      },
      handleFilter(params, done) {
        Object.assign(this.searchForm,params);
        this.getList();
        done()
      }
    }
  }
</script>
